package com.example.reagent_management.ui.message.adpter

import androidx.recyclerview.widget.DiffUtil
import com.example.lib_base.adapter.BasePagingAdapter
import com.example.lib_base.utils.DateConvertUtils
import com.example.reagent_management.R
import com.example.reagent_management.ui.message.pojo.Message

class MessageAdapter : BasePagingAdapter<Message>(object : DiffUtil.ItemCallback<Message>(){
    override fun areItemsTheSame(oldItem: Message, newItem: Message): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: Message, newItem: Message): Boolean {
        return oldItem == newItem
    }

}) {
    override fun getItemLayout(position: Int): Int {
        return R.layout.item_message
    }

    private var clickPosition: Int = -2
    private var allCount: Int = 0
    private var diffPosition = 0

    override fun bindData(helper: ItemHelper, data: Message?) {
        if (data != null){
            val split = data.messages.split("|")
            helper.setText(R.id.content,split[0])
            helper.setText(R.id.remindCount,split[1])
            val type = when (data.type){
               0-> "温/湿度提醒"
                1 -> "到期提醒"
                else -> "库存提醒"
            }
            helper.setText(R.id.type,type)
            helper.setText(R.id.date,DateConvertUtils.convertTimeToStringCH(data.time,"yyyy-MM-dd HH:mm:ss"))

//            if (clickPosition != -2  && position == clickPosition) {
//                setBackGround(R.id.items, R.drawable.bg_rgn_warning)
//            } else {
//                setBackGround(R.id.items, R.drawable.bg_message_item_select_false)
//            }
        }
    }

    override fun getClickViewId(): Array<Int>? {
        return null
    }

    //点击item背景颜色修改  暂定 更新旧点击位置和新点击位置  不必要notifyDataChange
    //记录点击item的位置，以及如果数据实时变化的话点击位置也要改变  adapter.
    fun setClickPosition(position: Int) {
        notifyItemChanged(clickPosition)
        this.clickPosition = position
        notifyItemChanged(position)
    }

    /**
     *
     *             //数据源状态CombinedLoadStates
    adapter.addLoadStateListener {
    //实时增加 删除时的背景
    //                Log.e("addLoadStateListener", "initViewOrObserve: $it" )
    if (it.source.prepend.endOfPaginationReached && remClickPosition != -1) {
    val insertOrDelete = adapter.itemCount - remOldCounts
    if (insertOrDelete > 0) {
    //增加数据
    remClickPosition += insertOrDelete
    } else if (insertOrDelete < 0) {
    if (canVisibleId == deleteId) {
    remClickPosition = -1
    } else if (canVisibleId < deleteId) {
    remClickPosition += insertOrDelete
    }
    if (remClickPosition < 0) remClickPosition = 0
    }
    adapter.setClickPosition(remClickPosition)
    remOldCounts = adapter.itemCount
    }
    }
     */


    ////是否有省略号
    //                        recyclerView.findContainingItemView(view)?.let { its ->
    //                            (its.findViewById<TextView>(R.id.notice_msg)).layout?.let { layout ->
    //                                if (layout.getEllipsisStart(0) > 0) {
    //                                    detailed.isVisible = true
    //                                    if (canVisibleId != it.id) {
    //                                        canVisibleId = it.id
    //                                        time.text = it.createTime
    //                                        description.text = it.msg
    //                                    }
    //                                } else {
    //                                    detailed.isVisible = false
    //                                    canVisibleId = it.id
    //                                }
    //                            }
    //                            if (remClickPosition != position) {
    //                                remClickPosition = position
    //                                remOldCounts = adapter.itemCount
    //                                adapter.setClickPosition(position)
    //                            }
    //                            //不复用,可见时背景颜色变回原来的颜色，不推荐，数据很多时列表为保障性能有自己的缓存策略
    ////                                    recyclerView.findViewHolderForLayoutPosition(position)?.setIsRecyclable(false)
    //                            //adapter.notifyDataSetChanged() 都刷新不必要
    //                        }
}